package com.ideaaedi.commonspring.spi;

import com.ideaaedi.commonds.monitor.WatchTimeUtil;
import com.ideaaedi.commonspring.filter.TimeWatcherFilter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.List;

/**
 * {@link TimeWatcherFilter}局部替换器
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2100.10.7.LTS17
 */
public interface TimeWatcherFilterReplacer {
    
    /**
     * 是否仅替换打印逻辑
     *
     * @return <ul>
     *     <li>true-仅替换打印逻辑</li>
     *     <li>false-替换整个{@link TimeWatcherFilter#doFilter}逻辑</li>
     * </ul>
     */
    boolean replacePrint();
    
    /**
     * 打印计时器结果
     * <pre>
     *     计时器相关数据的获取，可参考
     *     {@link WatchTimeUtil.Manager#prettyResult()}
     *     {@link WatchTimeUtil.Manager#result()}
     *     {@link WatchTimeUtil.Manager#getEndTaskList()}
     *     {@link WatchTimeUtil.Manager#sortByTaskNo(List)}
     * </pre>
     *
     * @param request 请求
     * @param prettyResult 是否需要美化打印结果
     */
    default void print(@Nullable HttpServletRequest request, boolean prettyResult) {
        if (replacePrint()) {
            throw new RuntimeException("please override method 'print' at class " + this.getClass().getName());
        }
    }
    
    /**
     * 替换整个{@link TimeWatcherFilter#doFilter}逻辑
     */
    default void doFilter(ServletRequest request, ServletResponse response, FilterChain chain, boolean prettyResult) throws IOException,
            ServletException {
        if (!replacePrint()) {
            throw new RuntimeException("please override method 'doFilter' at class " + this.getClass().getName());
        }
    }
}
